package c.c.c.a.i;

import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Printer;
import android.util.SparseArray;
import c.c.c.a.n;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/* compiled from: LooperMessageManager */
public class h implements Callback {
    public static Printer i;
    public static h j;
    public static final Printer k = new a();
    public Handler a = new Handler(j.a().getLooper(), this);
    public int b = 0;
    public long c;
    public final SparseArray<List<Runnable>> d = new SparseArray();
    public final List<Printer> e = new LinkedList();
    public final List<Printer> f = new LinkedList();
    public boolean g = false;
    public boolean h;

    /* compiled from: LooperMessageManager */
    public static class a implements Printer {
        public void println(String str) {
            Context context;
            c cVar;
            if (str != null) {
                h a;
                if (str.startsWith(">>>>> Dispatching")) {
                    a = h.a();
                    if (!a.h) {
                        i.a(32);
                        a.h = true;
                    }
                    a.c = SystemClock.uptimeMillis();
                    try {
                        a.d(a.e, str);
                        a.a.sendEmptyMessage(0);
                    } catch (Exception unused) {
                        context = n.a;
                        cVar = n.d;
                    }
                } else if (str.startsWith("<<<<< Finished")) {
                    a = h.a();
                    Objects.requireNonNull(a);
                    SystemClock.uptimeMillis();
                    try {
                        a.a.removeMessages(2);
                        a.d(a.f, str);
                        a.a.sendEmptyMessage(1);
                    } catch (Exception unused2) {
                        context = n.a;
                        cVar = n.d;
                    }
                }
                Printer printer = h.i;
                if (!(printer == null || printer == h.k)) {
                    h.i.println(str);
                }
            }
        }
    }

    public h() {
        e();
    }

    public static h a() {
        if (j == null) {
            synchronized (h.class) {
                if (j == null) {
                    j = new h();
                }
            }
        }
        return j;
    }

    public static void c(List<? extends Runnable> list) {
        if (list != null && !list.isEmpty()) {
            try {
                for (Runnable run : list) {
                    run.run();
                }
            } catch (Exception unused) {
                Context context = n.a;
                c cVar = n.d;
            }
        }
    }

    public void b(long j, Runnable runnable, int i, long j2) {
        if (j >= 0) {
            for (int i2 = 0; i2 < i; i2++) {
                int i3 = (int) j;
                List list = (List) this.d.get(i3);
                if (list == null) {
                    synchronized (this.d) {
                        list = (List) this.d.get(i3);
                        if (list == null) {
                            list = new LinkedList();
                            this.d.put(i3, list);
                        }
                    }
                }
                list.add(runnable);
                j += j2;
            }
        }
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:11:0x001e */
    /* JADX WARNING: Can't wrap try/catch for region: R(2:11|12) */
    /* JADX WARNING: Missing block: B:12:?, code skipped:
            r2 = c.c.c.a.n.a;
            r2 = c.c.c.a.n.d;
     */
    /* JADX WARNING: Missing block: B:19:0x0028, code skipped:
            return;
     */
    public final synchronized void d(java.util.List<? extends android.util.Printer> r2, java.lang.String r3) {
        /*
        r1 = this;
        monitor-enter(r1);
        if (r2 == 0) goto L_0x0027;
    L_0x0003:
        r0 = r2.isEmpty();	 Catch:{ all -> 0x0024 }
        if (r0 == 0) goto L_0x000a;
    L_0x0009:
        goto L_0x0027;
    L_0x000a:
        r2 = r2.iterator();	 Catch:{ Exception -> 0x001e }
    L_0x000e:
        r0 = r2.hasNext();	 Catch:{ Exception -> 0x001e }
        if (r0 == 0) goto L_0x0022;
    L_0x0014:
        r0 = r2.next();	 Catch:{ Exception -> 0x001e }
        r0 = (android.util.Printer) r0;	 Catch:{ Exception -> 0x001e }
        r0.println(r3);	 Catch:{ Exception -> 0x001e }
        goto L_0x000e;
    L_0x001e:
        r2 = c.c.c.a.n.a;	 Catch:{ all -> 0x0024 }
        r2 = c.c.c.a.n.d;	 Catch:{ all -> 0x0024 }
    L_0x0022:
        monitor-exit(r1);
        return;
    L_0x0024:
        r2 = move-exception;
        monitor-exit(r1);
        throw r2;
    L_0x0027:
        monitor-exit(r1);
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.c.a.i.h.d(java.util.List, java.lang.String):void");
    }

    public void e() {
        if (!this.g) {
            Printer printer;
            this.g = true;
            try {
                Field declaredField = Class.forName("android.os.Looper").getDeclaredField("mLogging");
                declaredField.setAccessible(true);
                printer = (Printer) declaredField.get(Looper.getMainLooper());
            } catch (Exception unused) {
                Context context = n.a;
                c cVar = n.d;
                printer = null;
            }
            i = printer;
            Printer printer2 = k;
            if (printer == printer2) {
                i = null;
            }
            Looper.getMainLooper().setMessageLogging(printer2);
        }
    }

    public boolean handleMessage(Message message) {
        if (this.a.hasMessages(0)) {
            return true;
        }
        int i = message.what;
        if (i == 0) {
            this.b = 0;
            if (this.d.size() != 0 && this.d.keyAt(0) == 0) {
                c((List) this.d.valueAt(0));
                this.b++;
            }
        } else if (i == 1) {
            this.a.removeMessages(2);
            if (this.d.size() != 0) {
                SparseArray sparseArray = this.d;
                if (sparseArray.keyAt(sparseArray.size() - 1) == 0) {
                    c((List) this.d.get(Integer.MAX_VALUE));
                }
            }
            return true;
        } else if (i == 2) {
            c((List) this.d.valueAt(this.b));
            this.b++;
        }
        if (this.b >= this.d.size()) {
            return true;
        }
        long keyAt = (long) this.d.keyAt(this.b);
        if (keyAt != 2147483647L) {
            this.a.sendEmptyMessageAtTime(2, this.c + keyAt);
        }
        return true;
    }
}
